import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes'
import NProgress from 'nprogress'
import { Message } from 'element-ui'

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach(function(to,from,next){

    NProgress.start()

    if(to.meta.public){
        next();
        return;
    }

    let token = localStorage.getItem("token");
    if(token){
        next();
        return;
    }
    Message.warning("未登录");
    NProgress.done();
    // 避免vue路由切换时，出现相同地址的多次跳转操作，提示错误
    if(from.name=="login"){
        next(false);
    }else{
        next("/login");
    }
    // next("/login");
})
router.afterEach(function(){
    NProgress.done()
})

export default router
